//
//  CLInputToolbar.m
//  CLInputToolbar
//
//  Created by JmoVxia on 2017/8/16.
//  Copyright © 2017年 JmoVxia. All rights reserved.
//

#import "CLInputToolbar.h"
//#import "UIView+CLSetRect.h"
#import "UIView+Geometry.h"
#import "YGExtendChain.h"
#import "SDAutoLayout.h"

#define ZX_RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

@interface CLInputToolbar ()<UITextViewDelegate>
/**文本输入框*/
@property (nonatomic, strong) UITextView *textView;
/**边框*/
@property (nonatomic, strong) UIView *edgeLineView;
/**顶部线条*/
@property (nonatomic, strong) UIView *topLine;
/**底部线条*/
@property (nonatomic, strong) UIView *bottomLine;
/**textView占位符*/
@property (nonatomic, strong) UILabel *placeholderLabel;
/**发送按钮*/
@property (nonatomic, strong) UIButton *sendButton;
/**键盘高度*/
@property (nonatomic, assign) CGFloat keyboardHeight;
/**发送回调*/
@property (nonatomic, copy) inputTextBlock inputTextBlock;


@end

@implementation CLInputToolbar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSLog(@"---%f",ZmScreenWidth_N());
        
        self.frame = CGRectMake(0,ZmScreenHeight_N()   - (ZmIsIphoneX_N() ? 0 : 0) , ZmScreenWidth_N(),  50);
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        [self initView];
        [self addNotification];
    }
    return self;
}
-(void)initView {
    self.backgroundColor = [UIColor whiteColor];
    //顶部线条
    self.topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.5)];
//    self.topLine.backgroundColor = ZX_RGB(173, 173, 173) ;
    [self addSubview:self.topLine];
    //底部线条
//    self.bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 1, self.width, 1)];
//    self.bottomLine.backgroundColor = ZX_RGBACOLOR(0, 0, 0, 0.2);
//    [self addSubview:self.bottomLine];
    //边框
    self.edgeLineView = [[UIView alloc]init];
    self.edgeLineView.width = self.width -  52 - 15 - 26;
    self.edgeLineView.left = 10;
//    self.edgeLineView.layer.cornerRadius = 5;
//    self.edgeLineView.layer.borderColor = ZX_RGBACOLOR(0, 0, 0, 0.5).CGColor;
//    self.edgeLineView.layer.borderWidth = 1;
//    self.edgeLineView.layer.masksToBounds = YES;
//    self.edgeLineView.layer.borderColor = ZX_RGB(153, 153, 153).CGColor;
//    self.edgeLineView.layer.borderColor = applineColor.CGColor;
    self.edgeLineView.layer.masksToBounds = YES;
    self.edgeLineView.layer.cornerRadius = 5 ;
    self.edgeLineView.layer.borderWidth = 0 ;
    self.edgeLineView.backgroundColor = [UIColor colorWithHexExtendString:@"F6F6F6"];
    [self addSubview:self.edgeLineView];
    
    //输入框
    self.textView = [[UITextView alloc] init];;
    self.textView.width = self.width -  52 - 15 - 36;
    self.textView.left = 18;
//    self.textView.enablesReturnKeyAutomatically = YES;
    self.textView.delegate = self;
    self.textView.layoutManager.allowsNonContiguousLayout = NO;
    self.textView.scrollsToTop = NO;
    self.textView.textContainerInset = UIEdgeInsetsZero;
    self.textView.textContainer.lineFragmentPadding = 0;

    self.textView.enablesReturnKeyAutomatically = YES;
    self.textView.returnKeyType = UIReturnKeySend;
    self.textView.backgroundColor = [UIColor clearColor] ;
//    self.textView.layer.cornerRadius = 14;
//    self.textView.layer.masksToBounds = YES;
    self.textView.textColor = [UIColor blackColor];
    
    [self addSubview:self.textView];
    
    //占位文字
    self.placeholderLabel = [[UILabel alloc] init];
    self.placeholderLabel.width = self.textView.width - 10;
    self.placeholderLabel.textColor = ZX_RGBACOLOR(0, 0, 0, 0.5);
    self.placeholderLabel.backgroundColor = [UIColor clearColor];
    self.placeholderLabel.left = 23;
    [self addSubview:self.placeholderLabel];
    
    //发送按钮
//    self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 50 - 10, self.height - 30 - 10, 50, 30)];
//    [self.sendButton.layer setBorderWidth:1.0];
//    [self.sendButton.layer setCornerRadius:5.0];
//    self.sendButton.layer.borderColor = ZX_RGBACOLOR(0, 0, 0, 0.5).CGColor;
//    self.sendButton.enabled = NO;
//    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:16];
//    [self.sendButton setTitle:@"发送" forState:UIControlStateNormal];
//    [self.sendButton setTitleColor:ZX_RGBACOLOR(0, 0, 0, 0.2) forState:UIControlStateNormal];
//    [self.sendButton addTarget:self action:@selector(didClicksendButton) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:self.sendButton];
//    [self addSubview:self.commentBtn];
//    [self addSubview:self.otherBtn];
    [self addSubview:self.sendBtn];
    
    self.fontSize = 16 ;
    self.textViewMaxLine = 3;
    
}

- (void)setChangColor:(int)changColor{
    _changColor = changColor;
    if (changColor == 1) {
        self.backgroundColor = [UIColor blackColor];
        self.textView.backgroundColor =  [UIColor clearColor];
        self.textView.textColor = [UIColor blackColor];
        self.placeholderLabel.textColor = [UIColor lightGrayColor];
        self.placeholderLabel.backgroundColor = [UIColor clearColor];
        self.edgeLineView.backgroundColor = [UIColor clearColor];
        self.topLine.backgroundColor = [UIColor clearColor] ;
        self.edgeLineView.backgroundColor = ZX_RGBACOLOR(52, 52, 52, 0.8);
//
//        [self.commentBtn setImage:[Theaty_IMG(@"comment_night_24x24_") imageWithChangeColor:[UIColor lightGrayColor]] forState:UIControlStateNormal ] ;
//        [self.otherBtn setImage:[Theaty_IMG(@"icon_details_collect_24x24_") imageWithChangeColor:[UIColor lightGrayColor]] forState:UIControlStateNormal ] ;
//        [self.sendBtn setImage:[Theaty_IMG(@"icon_details_share_press_24x24_") imageWithChangeColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];

    }
    
}

- (void)setCollectionHide:(int)collectionHide{
    _collectionHide = collectionHide;
    
    if (self.collectionHide == 1) {
        self.edgeLineView.width = self.width -  62 - 15 - 30 + 50;
        self.textView.width = self.width -  50 - 15 - 46 + 55;
        self.otherBtn.hidden = YES ;
        
    }
    
}
 
#pragma mark - lazy
- (UIButton *)sendBtn
{
    if(_sendBtn == nil)
    {
        _sendBtn = [[UIButton alloc]init];
//        [_sendBtn setImage:[Theaty_IMG(@"zx_share") imageWithChangeColor:appTextColor] forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(didClicksendButton) forControlEvents:UIControlEventTouchUpInside];
//        [_sendBtn setTitle:@"发送" forState:0];
//        [_sendBtn setTitleColor: [UIColor whiteColor ] forState:0];
//        _sendBtn.backgroundColor = [UIColor colorWithHexExtendString:@"#FFB324" ];
        _sendBtn.sh_chain_extend.title(@"发送").font_size(15).titleColor([UIColor colorWithHexExtendString:@"#016EFF"]);
        
    }
    return _sendBtn;
}
- (UIButton *)otherBtn{
    if (_otherBtn == nil) {
        _otherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_otherBtn setImage:[Theaty_IMG(@"zx_c1") imageWithChangeColor:appTextColor] forState:UIControlStateNormal ] ;
//        [_otherBtn setImage:[Theaty_IMG(@"zx_c2") imageWithChangeColor:appRedColor] forState:UIControlStateSelected ] ;
        _otherBtn.tag = 1 ;
        [_otherBtn addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside ] ;
    }
    return _otherBtn;
}
- (UIButton *)commentBtn{
    if (!_commentBtn) {
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_commentBtn setImage:[Theaty_IMG(@"zx_pl") imageWithChangeColor:appTextColor] forState:UIControlStateNormal ] ;
        _commentBtn.tag = 2 ;
        [_commentBtn addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside ] ;
        
        _commentCountBtn = [[UIButton alloc]init];

        //        _commentCountBtn.textColor = [UIColor whiteColor];
        [_commentCountBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _commentCountBtn.layer.masksToBounds = YES;
        _commentCountBtn.layer.cornerRadius = 7;
        _commentCountBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_commentBtn addSubview:_commentCountBtn];
        [_commentCountBtn setupAutoSizeWithHorizontalPadding:5 buttonHeight:14];
        _commentCountBtn.sd_layout.centerXEqualToView(_commentBtn).offset(12)
        .topEqualToView(_commentBtn).offset(-2).heightIs(14);
//        _commentCountBtn.hidden = YES;

    }
    return _commentBtn;
}

- (void)setCommtnet_count:(NSString *)commtnet_count{
    _commtnet_count = commtnet_count;
    if ([commtnet_count intValue]> 0) {
        _commentCountBtn.hidden = NO;
        [_commentCountBtn setTitle:commtnet_count forState:UIControlStateNormal];
    }else{
        _commentCountBtn.hidden = YES;
        [_commentCountBtn setTitle:@"" forState:UIControlStateNormal];
    }
}

//

- (void)sendBtnClick:(UIButton *)btn{
    
    if (self.btnsBlock) {
        self.btnsBlock(btn);
    }
}
// 添加通知
-(void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self.textView];

}
- (void)setEnd:(BOOL)end{
    _end = end;
    if (end == YES) {
        self.textView.userInteractionEnabled = NO ;
    }else{
        self.textView.userInteractionEnabled = YES ;
    }
    
}

-(void)setFontSize:(CGFloat)fontSize{
    _fontSize = fontSize;
    if (!fontSize || _fontSize < 15) {
        _fontSize = 15;
    }
    self.textView.font = [UIFont systemFontOfSize:_fontSize];
    self.placeholderLabel.font = self.textView.font;
    CGFloat lineH = self.textView.font.lineHeight;
    self.height =  50;
    self.textView.height = lineH;
 
    
}
- (void)setTextViewMaxLine:(NSInteger)textViewMaxLine {
    _textViewMaxLine = textViewMaxLine;
    if (!_textViewMaxLine || _textViewMaxLine <= 0) {
        _textViewMaxLine = 3;
    }
}
-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.placeholderLabel.text = placeholder;
}
#pragma mark keyboardnotification
- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardHeight = keyboardFrame.size.height;
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.top = keyboardFrame.origin.y - self.height  ;
    }];
}
- (void)keyboardWillHidden:(NSNotification *)notification {
    
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
//        self.top = ZmScreenHeight_N() - self.height  ;
        self.top = ZmScreenHeight_N()   ;

    }];
    
}
#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    self.placeholderLabel.hidden = textView.text.length;
    if (textView.text.length == 0) {
        self.sendButton.enabled = NO;
        [self.sendButton setTitleColor:ZX_RGBACOLOR(0, 0, 0, 0.2) forState:UIControlStateNormal];
    }else{
        self.sendButton.enabled = YES;
        [self.sendButton setTitleColor:ZX_RGBACOLOR(0, 0, 0, 1.0) forState:UIControlStateNormal];
    }
//    CGFloat contentSizeH = textView.contentSize.height;
    CGFloat contentSizeH = [textView.text boundingRectWithSize:CGSizeMake( textView.contentSize.width, MAXFLOAT) options:0  attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
//    CGFloat contentSizeH = [textView.text sizeWithFont:textView.font size:CGSizeMake(textView.contentSize.width, MAXFLOAT)].height;
    CGFloat lineH = textView.font.lineHeight;
    CGFloat maxTextViewHeight = ceil(lineH * self.textViewMaxLine + textView.textContainerInset.top + textView.textContainerInset.bottom);
    if (contentSizeH <= maxTextViewHeight) {
        textView.height = contentSizeH;
    }else{
        textView.height = maxTextViewHeight;
    }
    
    
    self.height = ZmIsIphoneX_N()  ? 10 + ceil(textView.height) + 10 + 10 + 10 : ceil(textView.height) + 10 + 10 + 10 ;
    
    
    self.bottom = ZmScreenHeight_N() -  _keyboardHeight  ;
    
    [textView scrollRangeToVisible:NSMakeRange(textView.selectedRange.location, 1)];
    
    
}
// 发送按钮
-(void)didClicksendButton {
    if (self.inputTextBlock) {
        self.inputTextBlock(self.textView.text);
    }
    [self bounceToolbar];

}
- (void)textDidChange
{
    if([self.textView.text containsString:@"\n"])
    {
        if (self.inputTextBlock) {
            self.inputTextBlock(self.textView.text);
        }
        [self bounceToolbar];
        return;
    }
    
}

- (void)inputToolbarSendText:(inputTextBlock)sendText{
    self.inputTextBlock = sendText;
}
- (void)popToolbar{
    self.fontSize = _fontSize;
    [self.textView becomeFirstResponder];
    
    
    for (int i = 0; i<self.textView.subviews.count; i++) {
        UIView *view = self.textView.subviews[i];
        if (view.backgroundColor != [UIColor clearColor]) {
            view.backgroundColor = [UIColor clearColor];
            NSLog(@"- 修改背景色");
        }
    }
    
    
}
// 发送成功 清空文字 更新输入框大小
-(void)bounceToolbar {
    self.textView.text = @"";
    self.placeholder = @"输入内容" ;
    [self.textView.delegate textViewDidChange:self.textView];
    [self endEditing:YES];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.edgeLineView.height = self.textView.height + 16 ;
    self.textView.top = 12 + 6 ;
    self.placeholderLabel.height = self.textView.height ;
    self.placeholderLabel.top = 12 + 6 ;

    self.edgeLineView.top = 12 ;
    self.bottomLine.top = self.height - 1 ;
    
    
    CGSize sendButtonSize = CGSizeMake(66, 34) ;
    self.sendBtn.width = sendButtonSize.width ;
    self.sendBtn.height = sendButtonSize.height ;
    self.sendBtn.left = self.width - sendButtonSize.width - 12 ;
    

//
//    CGSize commentButtonSize = CGSizeMake(50, 30) ;
//    self.commentBtn.width = commentButtonSize.width ;
//    self.commentBtn.height = commentButtonSize.height ;
//    self.commentBtn.left = self.width - commentButtonSize.width * 3 - 20 - 10;
//
//    if (self.collectionHide == 1) {
//        self.otherBtn.width = 0 ;
//        self.commentBtn.left = self.width - commentButtonSize.width * 2 - 20 ;
//    }
//
//    if (self.video == 1) {
//        self.edgeLineView.left = 10 + 60;
//
//        self.textView.left = 18 + 60;
//        
//        CGSize otherButtonSize = CGSizeMake(50, 40) ;
//        self.otherBtn.width = otherButtonSize.width ;
//        self.otherBtn.height = otherButtonSize.height ;
//        self.otherBtn.left = 10 ;
//        self.otherBtn.top = 6 ;
//        self.placeholderLabel.left = 23 + 60;
//
//    }
    self.sendBtn.top = 12 ;
//    self.otherBtn.centerY = self.height * 0.5 ;
//    self.commentBtn.centerY = self.height * 0.5 ;

//    self.sendBtn.layer.cornerRadius = sendButtonSize.height/2;
//    self.sendBtn.layer.masksToBounds = YES;
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

 
@end
